function setSegmentOnSegmentList(segment){
	alert("the segment has been sent to the controller");
	var id = new String(segment);
	window.parent.document.getSegmentFromPreview([id]);
}

function highLightSegment(segmentId){
	var segment = "http://0.0.0.0/?ID=" + segmentId;
	
	$(document).ready(function() {
	
		$('#preview').each(function() {
			$(".annotationLayer > a[class='selectedSegment']", this.contentWindow.document||this.contentDocument).removeClass("selectedSegment");
			$(".annotationLayer > a[href='" + segment + "']", this.contentWindow.document||this.contentDocument).addClass("selectedSegment");
		});
		
	});
}


function setPreviewAsDirty(message){

	var defaultMessage = "There are modified segment(s) waiting to be added to the pdf";
	if(typeof message === 'undefined'){
		var div="<div id=\"dirtyPreview\"><div class=\"dirtyPreview\">" + defaultMessage + "</div></div>";
	}else{
		var div="<div id=\"dirtyPreview\"><div class=\"dirtyPreview\">" + message + "</div></div>";
	}
	
	$(document).ready(function() {
		$('#preview').each(function() {
			$( "#dirtyPreview", this.contentWindow.document||this.contentDocument ).remove();
			$(".toolbar", this.contentWindow.document||this.contentDocument).append(div);
		});
	});
}


function showMessage(message, type){

	switch (type)
		{
		   case "success":
		   		var div="<div id=\"notificationBar\"><div class=\"notificationMessage success\">" + message + "</div></div>";
		   break;
		   case "info":
				var div="<div id=\"notificationBar\"><div class=\"notificationMessage info\">" + message + "</div></div>";
		   break;
		   case "warning": 
				var div="<div id=\"notificationBar\"><div class=\"notificationMessage warning\">" + message + "</div></div>";
		   break;
		   case "danger": 
				var div="<div id=\"notificationBar\"><div class=\"notificationMessage danger\">" + message + "</div></div>";
		   break;

		   default: 
		      	var div="<div id=\"notificationBar\"><div class=\"notificationMessage\">" + message + "</div></div>";
		   break;
		}

	$(document).ready(function() {
		$('#preview').each(function() {
			$( "#notificationBar", this.contentWindow.document||this.contentDocument ).remove();
			$(".toolbar", this.contentWindow.document||this.contentDocument).append(div);
		});
	});
}


function setThePDFFile(pdf){

	var domain = 'http://localhost:8080';
	var iframe = document.getElementById('preview').contentWindow;

	var message = pdf;
	console  .log("sending message");
	iframe   .postMessage(message, domain);

}

window.addEventListener('message', function(event){
	if(event.origin !== 'http://localhost:8080') return;
	console  .log('received response: ', event.data);
}, false);



$(document).ready(function() {
		var iframeDoc = $('#preview').contents().get(0);
			
		$(iframeDoc).on("click",'.annotationLayer a' , function (e) {
			clickHandler($(e.delegateTarget.activeElement));
	        e.preventDefault(); 
	    });
		

		$(iframeDoc).on("contextmenu", ".annotationLayer a", function( e ) {
			alert("rigth button clicked");
	        e.preventDefault(); 
	        $("<div class='custom-menu'>Custom menu</div>")
        		.appendTo("body")
        		.css({top: event.pageY + "px", left: event.pageX + "px"});
			});

		function clickHandler(segmentInfo, e) {
			var segmentClicked = new String(segmentInfo[0]);
	    	var segment = segmentClicked.substring(segmentClicked.indexOf("ID=") + 3);
	    	alert("You've clicked the segment: " + segment);
	    	highLightSegment(segment);
	        /*setSegmentOnSegmentList(segment);*/
	    }
	    
	});
